package com.colorlight.cloud.complete.entity.event;

import com.colorlight.cloud.complete.api.CompleteEntityEvent;
import com.colorlight.cloud.complete.entity.po.FunctionCardInfo;
import com.colorlight.cloud.complete.entity.po.M2Info;
import com.colorlight.iot.proto.complete.event.CompleteEvent;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 多功能卡上报
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class FunctionReport implements CompleteEntityEvent {

    /**
     * m2多功能卡
     */
    private M2Info m2Info;

    /**
     * 多功能卡详情
     */
    private List<FunctionCardInfo> functionCardInfo;

    @Override
    public FunctionReport byteConvertObject(ByteString bytes) {
        CompleteEvent.FunctionReport functionReport = null;
        try {
            functionReport = CompleteEvent.FunctionReport.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        Objects.requireNonNull(functionReport);
        this.setM2Info(new M2Info().formProto(functionReport.getM2Info()));
        List<FunctionCardInfo> collect = functionReport.getFunctionCardInfoList().stream()
                .map(e -> new FunctionCardInfo().formProto(e)).collect(Collectors.toList());
        this.setFunctionCardInfo(collect);
        return this;
    }
}

